<template>
	<view class="bgColor">
		<nacBer>
			<!-- view slot="left-dom">
				fasdg
			</view> -->
		</nacBer>
		<view class="bold">设置密码</view>
		<view class="describe">请设置您的登录密码</view>
		<view class="from_box">
			<u--form
				labelPosition="left"
				:model="userInfo"
				:rules="rules"
				ref="uForm"
			>
				<u-form-item
					prop="password"
					borderBottom
					ref="item1"
				>
				 <view class="userInfro_flex">
					 <view class="ico_right_10">
						 <u-icon name="lock" color="#2979ff" size="28"></u-icon>
					 </view>
					 <u--input
						v-if="passwordShow"
					 	type="text"
					 	v-model="userInfo.password"
					 	placeholder="请输入密码"
					 	border="none"
					 >
					 </u--input>
					 <u--input
					  v-else
					 	type="password"
					 	v-model="userInfo.password"
					 	placeholder="请输入密码"
					 	border="none"
					 >
					 </u--input>
					 <view class="" style="width: 100rpx;display: flex;align-items: center;justify-content: flex-end;">
						  <u-icon @click="passwordShowFun" :name="passwordShow? 'eye-fill' : 'eye-off'" color="#2979ff"></u-icon>
					 </view>
				 </view>
				</u-form-item>
				<u-form-item
					prop="newpassword"
					borderBottom
					ref="item1"
				>
				 <view class="userInfro_flex">
					 <view class="ico_right_10">
							<u-icon name="lock" color="#2979ff" size="28"></u-icon>
					 </view>
					 
						<u--input
							v-if="passwordShow"
							type="text"
							v-model="userInfo.newpassword"
							placeholder="请确认密码"
							border="none"
						>
						</u--input>
						<u--input
							v-else
							type="password"
							v-model="userInfo.newpassword"
							placeholder="请确认密码"
							border="none"
						>
						</u--input>
						<view class="" style="width: 100rpx;display: flex;align-items: center;justify-content: flex-end;">
							<u-icon @click="passwordShowFun" :name="passwordShow? 'eye-fill' : 'eye-off'" color="#2979ff"></u-icon>
						</view>
						
				 </view>
				</u-form-item>
			</u--form>
		</view>
		<view class="login_top_100">
			<u-button @click="nextFun" type="primary">{{ bottonisShow?'完成注册' : '修改密码' }}</u-button>
		</view>
	</view>
</template>

<script>
	import nacBer from '@/components/navBar/index'
	import { addUser, resetPwd } from "@/api/register.js"
	import { encrypt } from '@/utils/jsencrypt'
	export default {
		data() {
			return {
				bottonisShow: true,
				passwordShow: false,
				userInfo: {
					password: ''
				},
				rules: {
					password: {
						type: 'string',
						required: true,
						message: '请输入密码',
						trigger: ['blur']
					},
					newpassword: {
						type: 'string',
						required: true,
						message: '请确认密码',
						trigger: ['blur']
					},
				},
			}
		},
		components: {
			nacBer
		},
		onLoad: function (option) {
				this.userInfo.userType = option.userType;
				this.userInfo.username = option.userName;
				if (option.agencyId) {
					this.userInfo.agencyId = option.agencyId;
				}
				if (option.type) {
					this.bottonisShow = false
				} else {
					this.bottonisShow = true
				}
		},
		methods: {
			passwordShowFun() {
				this.passwordShow = !this.passwordShow
			},
			nextFun() {
				this.$refs.uForm.validate().then(res => {
					if (this.userInfo.newpassword != this.userInfo.password) {
						uni.showToast({
						  title: '两次密码输入不一样',
						  icon: "none",
						  duration: 2000,
						})
						return
					}
					(this.bottonisShow?addUser:resetPwd)(this.bottonisShow?this.userInfo:{
						password: this.userInfo.password,
						userName: JSON.parse(uni.getStorageSync('userInfo')).userName,
						userId: JSON.parse(uni.getStorageSync('userInfo')).userId
					}).then((data) => {
						if(!this.bottonisShow){
							uni.showToast({
							  title: '修改成功',
							  icon: "none",
							  duration: 2000,
								success: () => {
									setTimeout(() => {
										uni.removeStorageSync("token");
										uni.removeStorageSync("userInfoRole");
										uni.removeStorageSync("userInfo");
										uni.reLaunch({
											url: "/pages/login/index"
										})
									}, 500)
								}
							})
							return
						}
						uni.reLaunch({
							url: "/pages/login/index"
						})
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.from_box {
		width: 100%;
		padding: 0 80rpx;
		box-sizing: border-box;
	}
	.userInfro_flex {
		display: flex;
		width: 100%;
	}
	.login_top_100 {
		border-radius: 100rpx;
		width: 550rpx;
		overflow: hidden;
		margin: auto;
		margin-top: 20%;
	}
	.bold {
		font-size: 50rpx;
		font-weight: bold;
		margin: 50rpx 0 30rpx 50rpx;
	}
	.describe {
		margin-left: 50rpx;
		margin-bottom: 100rpx;
	}
	.ico_right_10 {
		margin-right: 30rpx;
	}
</style>